package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品功能")
    public Result save(@RequestBody DishDTO dto){
        log.info("新增菜品：{}", dto);

        dishService.insertWithFlavor(dto);

        //清理缓存数据
        String key = "dish_" + dto.getCategoryId(); //获取redis中的key
        redisTemplate.delete(key);

        return Result.success();
    }


    /**
     * 分页查询菜品
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分类查询功能")
    public Result<PageResult> page(DishPageQueryDTO dto){
        log.info("分页查询菜品：{}", dto);

        PageResult pageResult = dishService.page(dto);

        return Result.success(pageResult);
    }


    /**
     * 菜品删除功能
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品功能")
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除菜品：{}", ids);

        dishService.delete(ids);

        //清理缓存
        extracted();

        return Result.success();
    }

    private void extracted() {
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品视图")
    public Result<DishVO> selectById(@PathVariable Long id){
        log.info("根据id查询菜品视图，id：{}", id);

        DishVO dishVO = dishService.selectById(id);

        return Result.success(dishVO);
    }


    /**
     * 修改菜品
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dto){
        log.info("修改菜品：{}", dto);

        dishService.update(dto);

        //清理缓存
        extracted();

        return Result.success();
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类查询菜品")
    public Result<List<Dish>> selectByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品，id：{}", categoryId);

        List<Dish> dishList = dishService.selectByCategoryId(categoryId);

        return Result.success(dishList);
    }


    /**
     * 修改菜品状态
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result updateStatus(@PathVariable String status, Long id){
        log.info("修改菜品状态为:{}", status);

        dishService.updateStatus(status, id);

        //清理缓存
        String key = "dish_" + dishService.selectById(id).getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }
}
